#!/bin/sh
####  此脚本应该在command目录下执行

### 为了在任意路径执行该脚本，加入以下代码

WORK_PATH=$(pwd)
BASH_PATH=$(cd `dirname $0`; pwd)

##########################################

if [ ! -d "$BASH_PATH/../lib" -o ! -d "$BASH_PATH/../build" ];then
   echo "dependent dir don\`t exist!"
   exit
fi

BIN2="$BASH_PATH/prog_arg.bin"
BIN2_NAME="$BASH_PATH/prog_arg"
CFLAGS="-Wall -c -fno-builtin -W -Wstrict-prototypes \
      -Wmissing-prototypes -Wsystem-headers"
LIBS="-I $BASH_PATH/../lib -I $BASH_PATH/../lib/user -I $BASH_PATH/../fs"
OBJS="$BASH_PATH/../build/string.o $BASH_PATH/../build/syscall.o \
      $BASH_PATH/../build/stdio.o $BASH_PATH/../build/assert.o $BASH_PATH/start.o"
DD_IN2=$BIN2
DD_OUT="$BASH_PATH/../hd50M.img" 

BIN="$BASH_PATH/prog_no_arg.bin"
BIN_NAME="$BASH_PATH/prog_no_arg"
DD_IN=$BIN

nasm -f elf32 $BASH_PATH/start.S -o $BASH_PATH/start.o
ar  rcs $BASH_PATH/simple_crt.a $OBJS $BASH_PATH/start.o

gcc -m32  -fno-stack-protector   $CFLAGS $LIBS   $BIN2_NAME".c" -o $BIN2_NAME".o" 
ld  -melf_i386 $BIN2_NAME".o"   $BASH_PATH/simple_crt.a -o $BIN2

gcc -m32 $CFLAGS  $LIBS  $BIN_NAME".c"  -o $BIN_NAME".o"
ld  -melf_i386  -e main $BIN_NAME".o" $OBJS -o $BIN

SEC_CNT=$(ls -l $BIN|awk '{printf("%d", ($5+511)/512)}')
SEC_CNT2=$(ls -l $BIN2|awk '{printf("%d", ($5+511)/512)}')

if [ -f $BIN ];then
   dd if=$DD_IN  of=$DD_OUT bs=512 count=$SEC_CNT  seek=300 conv=notrunc
   dd if=$DD_IN2 of=$DD_OUT bs=512 count=$SEC_CNT2 seek=350 conv=notrunc
fi

